<template>
  <div class="menu-container">
    <el-card shadow="never">
      <div class="flex-start">
        <el-button v-permission="'menuManage-add'" size="small" type="primary" @click="add"> 新 增 </el-button>
      </div>
      <div class="menu-table">
        <el-table :data="treeData" row-key="id" border>
          <el-table-column prop="name" label="菜单名称" />
          <el-table-column prop="component" label="前端组件" />
          <el-table-column prop="url" label="权限地址">
            <template slot-scope="scope">
              <el-dropdown v-if="scope.row.url.split(',').length > 1">
                <span class="el-dropdown-link"> {{ strToArr(scope.row.url, true) }}<i class="el-icon-arrow-down el-icon--right" /> </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in strToArr(scope.row.url)" :key="index" icon="el-icon-location-outline">
                    {{ item.value }}
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <span v-else>{{ scope.row.url }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="排序" />
          <el-table-column prop="icon" label="图标" />
          <!-- <el-table-column prop="createTime" label="创建时间" /> -->
          <el-table-column label="类型">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.menuType === '1'" type="warning">系统</el-tag>
              <el-tag v-if="scope.row.menuType === '2'" type="success">菜单</el-tag>
              <el-tag v-if="scope.row.menuType === '3'">按钮</el-tag>
              <el-tag v-if="scope.row.menuType === '4'" type="info">字段</el-tag>
              <el-tag v-if="scope.row.menuType === '5'" type="warning">页面</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="208">
            <template slot-scope="scope">
              <el-button v-permission="'menuManage-add'" size="mini" type="text" @click="add(scope.row)"> 新增 </el-button>
              <el-button v-permission="'menuManage-edit'" size="mini" type="text" @click="edit(scope.row)"> 修改 </el-button>
              <el-button v-permission="'menuManage-delete'" size="mini" type="text" @click="del(scope.row)"> 删除 </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <Dialog ref="dialog" :update="getMenuList" :tree-list="treeList" :tree-data="treeData" />
  </div>
</template>

<script>
import { internetApi } from '@/api/api'

export default {
  components: {
    Dialog: () => import('./dialog')
  },
  data() {
    return {
      treeData: [],
      treeList: []
    }
  },

  computed: {},
  mounted() {
    this.getMenuList()
  },

  methods: {
    async getMenuList() {
      const { data } = await this.$request(internetApi.getMenuList)
      this.treeList = data
      this.treeData = this.$utils.arrayToTree(data, { pid: 'parentId', rootId: '0' })
    },
    // 权限地址：编辑格式处理
    strToArr(data, first) {
      const ds = data.split(',') || []
      const newArr = ds.map((item) => {
        return {
          value: item
        }
      })
      if (first) {
        return newArr[0].value
      } else {
        // newArr.shift()
        return newArr
      }
    },
    //新增
    add(row) {
      this.$refs.dialog.open('add', row)
    },
    //编辑
    edit(row) {
      this.$refs.dialog.open('edit', row)
    },
    //删除
    del(row) {
      //提示框二次确认
      this.$confirm(`你确定要删除${row.name}吗?`, { type: 'warning' })
        .then(async () => {
          await this.$request(internetApi.deleteMenu, { id: row.id })
          this.getMenuList()
          this.$message.success('操作成功')
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.menu-container {
  height: 100%;
}
.el-dropdown-link {
  cursor: pointer;
}
</style>
